package com.COMP3111H.project;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TextView;

public class ViewBookDetailsTab extends TabActivity {
	final int HELP = 0;
	private TabHost tabHost;
	private RadioGroup tabGroup;
	Button menu;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_book_details_tab);

		tabHost = getTabHost();
		tabGroup = (RadioGroup) findViewById( R.id.tabGroup_ViewBookDetailsTab );

		Bundle extras = getIntent().getExtras();
		String data = extras.getString("data");

		Intent[] tab = new Intent[4];
		for (int i = 0; i < 4; i++) {
			tab[i] = new Intent(this, ViewBookDetails.class);
			tab[i].putExtra("data", data);
			tab[i].putExtra("tab", i);
		}

		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("All", null)
				.setContent(tab[0]));

		tabHost.addTab(tabHost.newTabSpec("tab2")
				.setIndicator("Like New", null).setContent(tab[1]));

		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Good", null)
				.setContent(tab[2]));

		tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("Fair", null)
				.setContent(tab[3]));

		/*
		setTabColor(tabHost);
		tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
			public void onTabChanged(String tabId) {
				setTabColor(getTabHost());
			}
		});
		*/
		tabGroup.setOnCheckedChangeListener( new OnTabChangeListener() );
		menu = (Button) findViewById(R.id.book_details_menu);
	}

	@Override
    protected void onResume() {
		super.onResume();
		if (LoginUtility.checkLoginStatus(this)) {
			menu.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					 String[] choices={"Home","View My Profile","Help","Cancel"};  
					             AlertDialog dialog = new AlertDialog.Builder(ViewBookDetailsTab.this)  
					                     // .setIcon(android.R.drawable.btn_star)  
					                      .setTitle("Menu")  
					                      .setItems(choices, onselect_logged).create();  
					             dialog.show();  
				}
			});
		}
		else{
			menu.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					 String[] choices={"Home","Help","Cancel"};  
					             AlertDialog dialog = new AlertDialog.Builder(ViewBookDetailsTab.this)  
					                     // .setIcon(android.R.drawable.btn_star)  
					                      .setTitle("Menu")  
					                      .setItems(choices, onselect_not_logged).create();  
					             dialog.show();  
				}
			});
		}
	}

	OnClickListener onselect_logged = new OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			Intent myIntent;
			switch (which) {
			case 0:
				myIntent = new Intent(ViewBookDetailsTab.this, Home.class);
				startActivity(myIntent);
				break;
			case 1:
				myIntent = new Intent(ViewBookDetailsTab.this, My_Profile.class);
				startActivity(myIntent);
				break;
			case 2:
				myIntent = new Intent(ViewBookDetailsTab.this, Help.class);
				startActivity(myIntent);
				break;
			case 3:
				dialog.dismiss();
				break;
			}
		}
	};
	
	OnClickListener onselect_not_logged = new OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			Intent myIntent;
			switch (which) {
			case 0:
				myIntent = new Intent(ViewBookDetailsTab.this, Home.class);
				startActivity(myIntent);
				break;
			case 1:
				myIntent = new Intent(ViewBookDetailsTab.this, Help.class);
				startActivity(myIntent);
				break;
			case 2:
				dialog.dismiss();
				break;
			}
		}
	};
	
	private class OnTabChangeListener implements OnCheckedChangeListener{

		public void onCheckedChanged(RadioGroup arg0, int id) {
			// TODO Auto-generated method stub
			switch (id) {
            case R.id.tab1_ViewBookDetailsTab:
                tabHost.setCurrentTabByTag("tab1");
                break;
            case R.id.tab2_ViewBookDetailsTab:
                tabHost.setCurrentTabByTag("tab2");
                break;
            case R.id.tab3_ViewBookDetailsTab:
                tabHost.setCurrentTabByTag("tab3");
                break;
            case R.id.tab4_ViewBookDetailsTab:
            	tabHost.setCurrentTabByTag("tab4");
            	break;
 
            }
		}
		
	}
	public static void setTabColor(TabHost tabhost) {
		for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
			tabhost.getTabWidget().getChildAt(i)
					.setBackgroundColor(android.graphics.Color.WHITE); // unselected
			TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i)
					.findViewById(android.R.id.title);
			tv.setTextColor(android.graphics.Color.GREEN);
		}
		tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab())
				.setBackgroundColor(android.graphics.Color.GREEN); // selected
		TextView tv = (TextView) tabhost.getTabWidget()
				.getChildAt(tabhost.getCurrentTab())
				.findViewById(android.R.id.title);
		tv.setTextColor(android.graphics.Color.WHITE);
	}
}
